package com.ewhome.apartment.mvp.presenter;

import android.app.Application;

import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.baidu.android.pushservice.PushSettings;
import com.ewhome.apartment.BuildConfig;
import com.ewhome.apartment.app.GlobalHttpHandlerImpl;
import com.ewhome.apartment.app.constant.AppConstant;
import com.ewhome.apartment.app.utils.RxUtils;
import com.ewhome.apartment.mvp.model.entity.BaseData;
import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.ewhome.apartment.mvp.contract.LoginActivityContract;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.PermissionUtil;

import java.util.List;


@ActivityScope
public class LoginActivityPresenter extends BasePresenter<LoginActivityContract.Model, LoginActivityContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;

    @Inject
    public LoginActivityPresenter(LoginActivityContract.Model model, LoginActivityContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
    }
    public void testLogin(String usename,String password){
        mModel.testLogin(usename,password).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseData>(mErrorHandler) {
                    @Override
                    public void onNext(BaseData baseData) {
                        ArmsUtils.snackbarText(GlobalHttpHandlerImpl.decodeFromJNI(baseData.getCipher()));
                    }
                });
    }

    public void requestPhoneState(){
        PermissionUtil.readPhonestate(new PermissionUtil.RequestPermission() {
            @Override
            public void onRequestPermissionSuccess() {
                PushSettings.enableDebugMode(mApplication, BuildConfig.LOG_DEBUG);
                PushManager.startWork(mApplication, PushConstants.LOGIN_TYPE_API_KEY, AppConstant.BAIDU_PUSH_APIKEY);
            }

            @Override
            public void onRequestPermissionFailure(List<String> permissions) {

            }

            @Override
            public void onRequestPermissionFailureWithAskNeverAgain(List<String> permissions) {

            }
        },mRootView.getRxPermissions(),mErrorHandler);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

}
